/*
 * @Author: xzy 1453077527@qq.com
 * @Date: 2025-08-24 00:34:58
 * @LastEditors: xzy 1453077527@qq.com
 * @LastEditTime: 2025-08-26 17:02:51
 * @Description:
 */
package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.ClassExamRecordDao;
import com.xtedu.examproject.entity.ClassExamRecord;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ClassExamRecordDaoImpl implements ClassExamRecordDao {

    @Override
    public int insert(ClassExamRecord classExamRecord) throws Exception {
        String sql ="insert into class_exam_records (class_id,teacher_id,exam_id,theory_avg,practice_avg,exam_student_count,cross_stu_count,create_date) values (?,?,?,?,?,?,?,?)";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setInt(1, classExamRecord.getClassId());
            ps.setInt(2, classExamRecord.getTeacherId());
            ps.setInt(3, classExamRecord.getExamId());
            ps.setFloat(4,classExamRecord.getTheoryAvgScore());
            ps.setFloat(5,classExamRecord.getPracticeAvgScore());
            ps.setFloat(6,classExamRecord.getExamStudentCount());
            ps.setFloat(7,classExamRecord.getCrossStudentCount());
            ps.setDate(8,classExamRecord.getCreateDate());
            return ps.executeUpdate();
        }
    }

    @Override
    public int update(ClassExamRecord classExamRecord) throws Exception {
        String sql= "update class_exam_records set class_id=?,teacher_id=?,exam_id=?,theory_avg=?,practice_avg=?,exam_stu_count=?,cross_stu_count=?,create_date=? where class_id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setInt(1, classExamRecord.getClassId());
            ps.setInt(2, classExamRecord.getTeacherId());
            ps.setInt(3, classExamRecord.getExamId());
            ps.setFloat(4, classExamRecord.getTheoryAvgScore());
            ps.setFloat(5, classExamRecord.getPracticeAvgScore());
            ps.setFloat(6, classExamRecord.getExamStudentCount());
            ps.setFloat(7, classExamRecord.getCrossStudentCount());
            ps.setDate(8, classExamRecord.getCreateDate());
            ps.setInt(9, classExamRecord.getClassId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int delete(ClassExamRecord classExamRecord) throws Exception {
        String sql= "delete from class_exam_records where class_id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setInt(1, classExamRecord.getClassId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public ClassExamRecord findById(int id) throws Exception {
        String sql= "select * from class_exam_records where class_id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)){
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            ClassExamRecord classExamRecord = new ClassExamRecord();
            while(rs.next()){
                classExamRecord.setClassId(rs.getInt("class_id"));
                classExamRecord.setTeacherId(rs.getInt("teacher_id"));
                classExamRecord.setExamId(rs.getInt("exam_id"));
                classExamRecord.setTheoryAvgScore(rs.getFloat("theory_avg"));
                classExamRecord.setPracticeAvgScore(rs.getFloat("practice_avg"));
                classExamRecord.setExamStudentCount(rs.getInt("exam_student_count"));
                classExamRecord.setCrossStudentCount(rs.getInt("cross_stu_count"));
                classExamRecord.setCreateDate(rs.getDate("create_date"));
                classExamRecord.setClassId(rs.getInt("class_id"));
            }
            return classExamRecord;
        }
    }

    @Override
    public List<ClassExamRecord> findAll() throws Exception {
        String sql= "select * from class_exam_records";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)){
            ResultSet rs = ps.executeQuery();
            List<ClassExamRecord> classExamRecords = new ArrayList<>();
            while (rs.next()){
                ClassExamRecord classExamRecord = new ClassExamRecord();
                classExamRecord.setClassId(rs.getInt("class_id"));
                classExamRecord.setTeacherId(rs.getInt("teacher_id"));
                classExamRecord.setExamId(rs.getInt("exam_id"));
                classExamRecord.setTheoryAvgScore(rs.getFloat("theory_avg"));
                classExamRecord.setPracticeAvgScore(rs.getFloat("practice_avg"));
                classExamRecord.setExamStudentCount(rs.getInt("exam_student_count"));
                classExamRecord.setCrossStudentCount(rs.getInt("cross_stu_count"));
                classExamRecord.setCreateDate(rs.getDate("create_date"));
                classExamRecord.setClassId(rs.getInt("class_id"));
                classExamRecords.add(classExamRecord);
            }
            return classExamRecords;
        }
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }
}
